package com.boc.ewr.cornJob;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.Pro;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.business.entity.ProPrbsdList;
import com.boc.ewr.business.service.IProPrbsdListService;
import com.boc.ewr.risk.entity.CaUnpriaccUnusual;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import com.boc.ewr.util.file.enumeration.ProSendStatusEnum;
import com.boc.ewr.util.msg.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.ComboModel;
import org.jeecg.common.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

/**
 * @author : Wzz
 * @create : 2023-04- 20- 15:26
 * @desc :问题提示-未签约账户-识别未签约账户并提示二级行
 */
@Slf4j
public class UnSignAcctJob implements Job {

    @Autowired
    private IProService proService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IProPrbsdListService proPrbsdListService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(" Job Execution key：" + jobExecutionContext.getJobDetail().getKey());
        log.info(String.format(" 未签约账户识别与提示 UnSignAcctJob 时间:" + DateUtils.getTimestamp()));

        // 清除之前数据
        proPrbsdListService.remove(
                new LambdaQueryWrapper<ProPrbsdList>()
                        .eq(ProPrbsdList::getSendSta,ProSendStatusEnum.UNSIGN.getIndex())
        );

        /*
        * 1.每月1-24日，取签约类型为0，协议签约时间中前10位数的年月日，
        *   用当天的日期减去签约日期≥5天，自动生成预警清单
        *   字段为：二级行名称|签约行名称|协议号|主账号|关联账号|客户号|客户名称
        * 2.每月25日起，发送全量未签约数据
        * */
        Date date = null;
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当月的25日
        LocalDate twentyFifth = LocalDate.of(currentDate.getYear(), currentDate.getMonth(), 25);
        // 判断当前日期是否在25日之前
        boolean isBefore25th = currentDate.isBefore(twentyFifth);
        if (isBefore25th) {
            // 获取当天之前5天的日期
            LocalDate localDate = currentDate.minusDays(5);
            date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }

        // 24号后发送全量未签约数据
        List<Pro> unSignAcctList = proService.getUnSignAcct(date);
        // 推送结果保存至问题提示-协议清单
        List<ProPrbsdList> sendList = new ArrayList<>();
        for (Pro pro : unSignAcctList) {
            // 推送结果保存至问题提示协议清单
            ProPrbsdList proPrbsdList = new ProPrbsdList();
            BeanUtils.copyProperties(pro, proPrbsdList);
            proPrbsdList.setSendSta(ProSendStatusEnum.UNSIGN.getIndex());
            sendList.add(proPrbsdList);
        }
        proPrbsdListService.saveOrUpdateBatch(sendList);

        this.sendMsg(sendList);
    }

    private void sendMsg(Collection<ProPrbsdList> entityList) {
        // 获取推送用户
        Set<String> branchNoSet = new HashSet();
        for (ProPrbsdList proPrbsdList : entityList) {
            branchNoSet.add(proPrbsdList.getSignedOrg());
        }
        String toUser = ToUserUtil.genToUser(branchNoSet);
        // String toUser = "admin";
        if (toUser!=null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "问题提示-未签约账户",
                    "协议清单中存在未签约账户，请及时查看并确认是否存在问题",
                    MsgCategoryEnum.TEMP_WORK.getIndex()
            );
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }
    }
}
